package com.dpkit.manager.factory;

import com.dpkit.adapter.DatabaseDialect;
import com.dpkit.adapter.OrmAdapter;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DpkitFactory {

    public static Map<String, Class<? extends DatabaseDialect>> DIALECT_MAP = new ConcurrentHashMap<>(16);

    public static Map<String, Class<? extends OrmAdapter>> ORM_MAP = new ConcurrentHashMap<>(16);

    public static OrmAdapter createOrmAdapter(DpKitConfiguration config) {
        try {
            Class<? extends OrmAdapter> adapterClass = ORM_MAP.get(config.getPersistence().getOrm());
            Class<? extends DatabaseDialect> dialectClass = DIALECT_MAP.get(config.getPersistence().getDatabase());
            
            DatabaseDialect dialect = dialectClass.getDeclaredConstructor().newInstance();
            return adapterClass.getDeclaredConstructor(ApplicationConfig.class, DatabaseDialect.class)
                .newInstance(config, dialect);
        } catch (Exception e) {
            throw new RuntimeException("Failed to create ORM adapter", e);
        }
    }
}